<!DOCTYPE html>
<html lang="zh-CN" ng-app='he'>
	<head>
		<meta charset='utf-8' />
	
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<title>请假申请单</title>
		<script type="text/javascript">
			is_local='{{local}}';
			function loadcss(cssfile) {
		        document.write('\<link rel="stylesheet" href="' + cssfile +'" type="text/css"/\>');
		    }
			function loadjs(script_filename) {
		        document.write('\<script language="javascript" type="text/javascript" src="' + script_filename + '"\>\</script\>');
		    }
		    if(!is_local){
			    static_url='/static';
		    }else{
			    static_url='../../../../static';
		    }
		    // 加载所有微信页面需要的js和css
		    loadjs(static_url+'/js/weixin_page.js')
		</script>
		
		<script type="text/javascript">
			
			var app = angular.module('he',['ngSanitize']);
			app.config(function($interpolateProvider) {
				  $interpolateProvider.startSymbol('[[');
				  $interpolateProvider.endSymbol(']]');
				});
			app.controller('content',function ($scope,$http) {
				// 设置body里面的变量
				$scope.leave={ category:'',
                        start_time:'',
                        end_time:'',
                        reason:''};
				if(is_local){
					var leave_record_url='';
					$scope.this_left=0.34;
					$scope.last_left=2.3;
					$scope.swap_left=23.5;
					$scope.submit=function () {
						submit_is_clicked=true;
						if($scope.form.$valid){
							alert('模拟提交成功');
							}
						}
				}else{
					// 从服务器初始化
					var post_url='/attendance/ajax/';
					function init_from_server() {
							var leave=$scope.leave;
							var post_data = {
									get_annual_left:{},
									get_swap_left:{}
								};  
							$http.post(post_url,post_data)
							.success(function(data, status, headers, config) { 
								$scope.this_left=data.get_annual_left.this_left;
								$scope.last_left=data.get_annual_left.last_left;
								$scope.swap_left=data.get_swap_left.swap_left;
							}).error(function(data, status, headers, config) {  
							    //处理错误 
							    $scope.std='有错误,返回码为:'+status;
							});
					}
					init_from_server();
					//--------提交服务器-----
					$scope.submit=function () {
						submit_is_clicked=true;
						if(!$scope.form.$valid)return
						var leave = $scope.leave;
						var postData = {
								submit_leave:{category:leave.category,start_time:dt2str( leave.start_time),end_time:dt2str(leave.end_time),
								reason:leave.reason},
							};  
						$http.post(post_url,postData)
						.success(function(data, status, headers, config) { 
							var status=data.submit_leave.status;
							if (status=='success'){
								info('successful submit',function () {
									location='leave_record'
								})
							}else{
								info(data.submit_leave.msg)
							}
							
					    
						}).error(function(data, status, headers, config) {  
						    //处理错误 
						    $scope.std='有错误,返回码为:'+status;
						});
					}
					//-----------------
				}
				var submit_is_clicked=false;
				$scope.is_need_warn=function (name){
					if(!submit_is_clicked) return false
					return $scope.form[name].$error.required
				}
				$scope.get_class=function (name) {
					if($scope.is_need_warn(name)){
						return 'warn_border'
					}else{
						return ''
					}
				}
				$scope.get_leader_list=function () {
					if($scope.leave.category&&$scope.leave.start_time&&$scope.leave.end_time){
						var postData = {
								get_app_check_name_list:{category:$scope.leave.category,
														start_time:dt2str($scope.leave.start_time),
														end_time:dt2str($scope.leave.end_time)},
							};  
						$http.post(post_url,postData)
						.success(function(data, status, headers, config) { 
							$scope.namelist=data.get_app_check_name_list.namelist
						}).error(function(data, status, headers, config) {  
						    //处理错误 
						    $scope.std='有错误,返回码为:'+status;
						});
					}

				}
				$scope.$watch('leave.category',function (newval,oldval) {
						$scope.get_leader_list()
				})
				$scope.$watch('leave.start_time',function (newval,oldval) {
						$scope.get_leader_list()
				})
				$scope.$watch('leave.end_time',function (newval,oldval) {
						$scope.get_leader_list()
				})
				
				$scope.test_click=function () {
					info('hello');
				}
			})
		function dt2str(dtobj) {
			return dtobj.getFullYear()+'-'+dtobj.getMonth()+'-'+dtobj.getDate()+" " +dtobj.getHours()+':'+dtobj.getMinutes()
		}
		</script>
	<style type="text/css">
		.warn_fore{
			color: #ff6014;
		}
		.warn_border{
			border-color:#ff6014;
		}
	</style>
	</head>
	<body ng-controller='content'>
		<!-----------------弹出框------------------------>
		<div ng-include src='"info_panel.html"'></div>
		<!-----------------在这里填写内容------------------------>
	<div class="container">
        <div class="row">
            <div class="col-xs-12">
                <form role="form" name="form" class="leaveForm" novalidate>
                    <p style="color: #1686E2;font-size: 1.6em">Leave Application</p>
                    <div>
                        <label class="tag">Reason<i>*</i></label>
                        <!-- [true] leave.reason==""  ng-class='{false:"normal",true:"warn"}[leave.reason==""]'-->
                        <textarea ng-class='get_class("reason")' class="form-control" name='reason'  ng-model='leave.reason' required></textarea>
                        <span ng-show="is_need_warn('reason')" class='warn_fore'>必填填写原因</span>
                    </div>
                    <div id="type">
                        <label class="tag">Type<i>*</i> </label>
                        <!-- ng-init="leave.category = 'select'"-->
                        <select class="form-control" name="leaveType" ng-model='leave.category' ng-class='get_class("leaveType")'  required>
                            <option value="">Select...</option>
                            <option value="personal_leave">Personal Leave(事假)</option>
                            <option value="sick_leave">Sick Leave(病假)</option>
                            <option value="annual_leave">Annual Leave(年假)</option>
                            <option value="swap_off">Swap off day(调休)</option>
                            <option value="out_of">Out of office(外出办公)</option>
                            <option value="miss_check">Miss Check(漏打卡)</option>
                            <option value="birthday_leave">Birthday Leave(生日假)</option>
                            <option value="marital_leave">Marital Leave(婚假)</option>
                            <option value="maternity_leave">Maternity Leave(产假)</option>
                            <option value="paternity_leave">Paternity Leave(陪产假)</option>
                            <option value="bereavement_leave">Bereavement Leave(丧假)</option>
                            <option value="business">Business Trip(出差)</option>
                            <option value="others">Others(其他)</option>
                        </select>
                        <span ng-show="is_need_warn('leaveType')" class='warn_fore'>必填选择申请类型</span>
                    </div>
                    <!------当选中年假和调休时，会显示相应的剩余时间---------->
                    <p ng-show="leave.category=='annual_leave'" class="hint">annual leave left: last year:<span ng-bind='last_left'></span> days ;this year:<span ng-bind='this_left'></span> days </p>
                    <p ng-show="leave.category=='swap_off'" class="hint">Your swap off remaining:<span ng-bind='swap_left'></span> hours</p>
                    <div>
                        <label class="tag">Start Time<i>*</i> </label>
                        <input type="datetime-local" class="form-control" name="startTime" ng-model='leave.start_time' required ng-class='get_class("startTime")'/>
                        <span ng-show="is_need_warn('startTime')" class='warn_fore'>必填选择申请的开始时间</span>
                    </div>
                    <div>
                        <label class="tag">End Time<i>*</i> </label>
                        <input type="datetime-local" class="form-control" name="endTime" ng-model='leave.end_time' ng-class='get_class("endTime")' required/>
                        <span ng-show="is_need_warn('endTime')" class='warn_fore'>必填选择申请的结束时间</span>
                    </div>
                    <div>
                        <label class="tag">Approver</label>
                        <div class="approverBox">
	                        <span class="approverName" ng-repeat='name in namelist' ng-bind='name'></span>
                            <!--<span class="approverName">Echo</span>
                            <span class="approverName">Liuhailong</span>-->
                        </div>
                    </div>
                    <!-- onclick="checkAll()"-->
                    <button type="button" class="btn btn-default submitBtn"  ng-click='submit()'>SUBMIT</button>
                </form>
            </div>
        </div>
    </div>

	</body>
</html>